package com.ling.ling_push_player;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import com.ywl5320.wlmedia.WlMedia;
import com.ywl5320.wlmedia.enums.WlPlayModel;
import com.ywl5320.wlmedia.listener.WlOnCompleteListener;
import com.ywl5320.wlmedia.listener.WlOnErrorListener;
import com.ywl5320.wlmedia.listener.WlOnPreparedListener;
import com.ywl5320.wlmedia.listener.WlOnTimeInfoListener;
import com.ywl5320.wlmedia.listener.WlOnVideoViewListener;
import com.ywl5320.wlmedia.surface.WlSurfaceView;

public class LingVideo {
    private String TAG = this.getClass().getSimpleName();
    /**
     * 视频时长
     */
    private double Duration;
    private static LingVideo instance;
    /**
     * 用于播放视频 创建一次播放器（避免系统底层GC导致的崩溃？）
     * 在APP退出时调用@releaseAndExit释放资源
     * @return
     */
    public static LingVideo getInstance(WlSurfaceView wlSurfaceView,Context context) {
        if(instance == null)
        {
            synchronized (LingVideo.class)
            {
                if(instance == null)
                {
                    instance = new LingVideo(wlSurfaceView,context);
                }
            }
        }
        return instance;
    }
        //单例模式主要用于视频，音频可以new对象
        WlMedia wlMedia;
        WlSurfaceView wlSurfaceView;
        Context context;

    public LingVideo(WlSurfaceView wlSurfaceView, Context context) {
        this.wlMedia= WlMedia.getInstance();
        this.wlSurfaceView=wlSurfaceView;
        this.context=context.getApplicationContext();
        wlMedia.setPlayModel(WlPlayModel.PLAYMODEL_AUDIO_VIDEO);//同时播放音频视频
    }

    public void  StartPlay(final String url){
        if (url==null){
            return;
        }
       wlSurfaceView.setWlMedia(wlMedia);//给视频surface设置播放器
        //异步准备完成后开始播放
        wlMedia.setOnPreparedListener(new WlOnPreparedListener() {
            @Override
            public void onPrepared() {
                wlMedia.start();//开始播放
                //获取时长（如果有在异步准备好后可获取）
                Duration=wlMedia.getDuration();
                Log.d(TAG,"Duration:"+Duration);
            }
        });

        //surface初始化好了后 开始播放视频（用于一打开activity就播放场景）
        wlSurfaceView.setOnVideoViewListener(new WlOnVideoViewListener() {
            @Override
            public void initSuccess() {
                wlMedia.setSource(url);//设置数据源
                wlMedia.prepared();//异步开始准备播放

            }

            @Override
            public void moveSlide(double value) {

            }

            @Override
            public void movdFinish(double value) {
                wlMedia.seek(value);//seek
                Log.d(TAG,"seek value:"+value);
            }
        });

        wlMedia.setOnErrorListener(new WlOnErrorListener() {
            @Override
            public void onError(int code, String msg) {
                Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
            }
        });
        //播放时间回调
        wlMedia.setOnTimeInfoListener(new WlOnTimeInfoListener() {
            @Override
            public void onTimeInfo(double currentTime) {
                //返回当前播放时间时间
                Log.d(TAG,"onTimeInfo:"+currentTime);
                if (isPlay()&&ISSEEk&&seekBar!=null){
                    //在主线程上执行更新操作
                    PlayActivity.MAINACTIVITY_HANDLER.post(new Runnable() {
                        @Override
                        public void run() {
                            seekBar.setVisibility(View.VISIBLE);
                            ISSEEk=false;
                        }
                    });
                }
            }
        });

        wlMedia.setOnCompleteListener(new WlOnCompleteListener() {
            @Override
            public void onComplete() {
                Log.d(TAG,"setOnCompleteListener");
                Toast.makeText(context,"播放完毕",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void release(){
        wlMedia.stop();
        wlMedia.releaseAndExit();
    }
    public void StopVideo(){
        if (isPlay()){
            wlMedia.stop();
        }
    }
    private boolean pause=false;
    public void PauseVideo(){
        if (isPlay()){
            if (!pause) {
                wlMedia.pause();
                pause=true;
            }else {
                wlMedia.resume();
                pause=false;
            }
        }
    }
    public boolean isPlay(){
        return  wlMedia.isPlay();
    }
    //获取当前播放时间
    public double getNowClock(){
        return  wlMedia.getNowClock();
    }
    //获取视频时长
    public double getDuration(){
        return  Duration;
    }

    private boolean ISSEEk=false;
    private SeekBar seekBar;
    public void Seek(SeekBar seekBar, int seek){
        this.seekBar=seekBar;
        double f;
        f= Duration*(seek/100f);
        Log.d(TAG,"Seek seek: "+(seek/100f));
        Log.d(TAG,"Seek Duration: "+Duration);
        Log.d(TAG,"Seek f: "+f);
        wlMedia.seek(f);
        //在主线程上执行更新操作
        PlayActivity.MAINACTIVITY_HANDLER.post(new Runnable() {
            @Override
            public void run() {
                seekBar.setVisibility(View.INVISIBLE);
                ISSEEk=true;
            }
        });
    }

}
